<?php
class SMSController {
# this just remove the country code
    function num_format($msisdn) {
        if (preg_match("/^(\+63|63|0)([0-9]{1,12})/" , trim($msisdn), $matches))
            return "0".$matches[2];
        else
            return $msisdn;
    }

    function validate_msisdn($msisdn) {
        $msisdn = $this->txt2num($msisdn);
        if (preg_match("/^(\+63|63|0)([0-9]{1,12})/" ,$msisdn, $matches)) return "0".$matches[2];
        return false;
    }


# convert text to its numeric equivalent and remove other character returning just numeric value
    function txt2num($i) {
        $pattern = array('/i/i','/l/i','/o/i','/[^\d]/');
        $replace = array(1,1,0,'');
        return preg_replace($pattern, $replace, $i);
    }
# convert numeric value to its equivalent aphacharacter (zero to letter O and number one to letter L)
    function num2txt($i) {
        $pattern = array('/1/i','/0/i');
        $replace = array('l','o');
        return preg_replace($pattern, $replace, $i);
    }

# this function assume that validation are done with mobile no.  and message

    function send_sms($msisdn, $body, $outbox_id, $smsc='') {
        $smsc = ($smsc) ? $smsc : $this->smsc;
        if ($msisdn && $body) {
            $dlr = DLR_URL . '&outbox_id=' . $outbox_id;
            $smsc = ""; //DISABLE SMSC USE RAMDOM SENDING;
            $url = SEND_URL . '&to=' . $msisdn . '&text=' . urlencode($body) . '&smsc=' . $smsc . '&dlr-url=' . urlencode($dlr);
            $ch = curl_init ($url);
            ob_start();
            curl_exec($ch);
            $str = ob_get_contents();
            ob_end_clean();
            curl_close ($ch);
            return $str;
        }
    }

}
?>
